/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import com.restfb.DefaultFacebookClient;
import com.restfb.FacebookClient;
import com.restfb.Parameter;
import com.restfb.types.FacebookType;
/**
 *
 * @author Dan
 */
public class FbUtil {
    
    private static FacebookClient facebookClient;


	public static String sendComent(String token, String content) {
		facebookClient = new DefaultFacebookClient(token);
		FacebookType publishMessageResponse = facebookClient.publish("me/feed",
				FacebookType.class, Parameter.with("message", content));
		return publishMessageResponse.getId();
	}

	public static String getTokenByCode(String code){
		String[] token = null;
		try {
			URL url = new URL("https://graph.facebook.com/oauth/access_token?client_id=131380196974144&redirect_uri=http://www.facebook.com/connect/login_success.html&client_secret=af2851a6ab96904a12ed1b8a98ae3939&code="+code);// A url usada é construida em outro método
									// para simplificar leitura do código
			System.out.println(url);
			HttpURLConnection connection = (HttpURLConnection) url
					.openConnection();
			try {
				connection.setDoOutput(true);
				connection.setInstanceFollowRedirects(false);
				connection.setRequestMethod("GET");
				connection.setRequestProperty("Content-Type",
						"text/plain;charset=UTF-8");
				BufferedReader in = new BufferedReader(new InputStreamReader(
						connection.getInputStream(), "UTF-8"));
				String line = null;
				while ((line = in.readLine()) != null) {
					
					token = line.split("=");
		
	}
			}catch (Exception e) {
				// TODO: handle exception
			}
		}catch (Exception e) {
			// TODO: handle exception
		}finally{
			return token[1];
		}
	}
        
}
